package com.exam.controller;

import com.exam.entity.*;
import com.exam.entity.common.ResponseResult;
import com.exam.service.*;
import com.exam.util.JsonUtils;
import com.exam.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class ExamController {

    @Autowired
    @Qualifier("examServiceImpl")
    private ExamService examService;

    @Autowired
    @Qualifier("examUserServiceImpl")
    private ExamUserService examUserService;

    @Autowired
    @Qualifier("singleChoiceQServiceImpl")
    private SingleChoiceQService singleChoiceQService;
    @Autowired
    @Qualifier("judgmentQServiceImpl")
    private JudgmentQService judgmentQService;

    @Autowired
    @Qualifier("shortAnswerQServiceImpl")
    private ShortAnswerQService shortAnswerQService;

    @Autowired
    @Qualifier("fillBlanksQServiceImpl")
    private FillBlanksQService fillBlanksQService;

    ResponseResult responseResult = new ResponseResult();

    @RequestMapping("/release_exam")
    public String release_exam() {
        return "views/manage/release_exam";
    }

    @RequestMapping("/exam_list")
    public String exam_list() {
        return "views/manage/exam_list";
    }

    @RequestMapping("/add_exam")
    @ResponseBody
    public String add_exam(Exam exam, String u_ids) {
        String[] u_id_data = u_ids.split(",");
        exam.setE_creat_time(TimeUtils.getTime());
        int i = examService.add_exam(exam);
        if (i == 1) {
            List<ExamUser> examUsers = new ArrayList<>();
            for (String u_id : u_id_data) {
                ExamUser examUser = new ExamUser();
                examUser.setE_id(exam.getE_id());
                examUser.setU_id(Integer.parseInt(u_id));
                examUser.setEu_state("未完成");
                examUsers.add(examUser);
            }
            int i1 = examUserService.add_exam_user(examUsers);
            if (i1 > 0) {
                responseResult.setCode(200);
                responseResult.setMessage("添加成功");
            } else {
                responseResult.setCode(0);
                responseResult.setMessage("添加失败");
            }

        } else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);

    }


    @RequestMapping("/begin_exam")
    public String begin_exam(int e_id, int eu_id, Model model) {
        model.addAttribute("e_id", e_id);
        model.addAttribute("eu_id", eu_id);
        return "views/user/exam";
    }


    @RequestMapping("/get_exam_by_e_id")
    @ResponseBody
    public String get_exam_by_e_id(int e_id) {
        Exam examByEId = examService.get_exam_by_e_id(e_id);
        Map<String, Object> map = new HashMap<>();
        map.put("exam", examByEId);
        String ep_scq_c_ids = examByEId.getExamPaper().getEp_scq_c_ids();
        String[] scq_c_ids = ep_scq_c_ids.split(",");
        List<Object> scq_data = new ArrayList<>();
        for (String c_id : scq_c_ids) {
            if (!c_id.equals("")) {
                SingleChoiceQuestions scqByCId = singleChoiceQService.get_scq_by_c_id(Integer.parseInt(c_id));
                if (scqByCId==null){
                    continue;
                }
                scq_data.add(scqByCId);
            }
        }
        map.put("scq_data", scq_data);

        String ep_jq_j_ids = examByEId.getExamPaper().getEp_jq_j_ids();
        String[] jq_j_ids = ep_jq_j_ids.split(",");
        List<Object> jq_data = new ArrayList<>();
        for (String j_id : jq_j_ids) {
            if (!j_id.equals("")) {
                JudgmentQuestions jqByJId = judgmentQService.get_jq_by_j_id(Integer.parseInt(j_id));
                if (jqByJId==null){
                    continue;
                }
                jq_data.add(jqByJId);
            }
        }
        map.put("jq_data", jq_data);

        String ep_saq_sa_ids = examByEId.getExamPaper().getEp_saq_sa_ids();
        String[] saq_sa_ids = ep_saq_sa_ids.split(",");
        List<Object> saq_data = new ArrayList<>();
        for (String sa_id : saq_sa_ids) {
            if (!sa_id.equals("")) {
                ShortAnswerQuestions saqBySaId = shortAnswerQService.get_saq_by_sa_id(Integer.parseInt(sa_id));
                if (saqBySaId==null){
                    continue;
                }
                saq_data.add(saqBySaId);
            }
        }
        map.put("saq_data", saq_data);

        String ep_fbq_fb_ids = examByEId.getExamPaper().getEp_fbq_fb_ids();
        String[] fbq_fb_ids = ep_fbq_fb_ids.split(",");
        List<Object> fbq_data = new ArrayList<>();
        for(String fb_id:fbq_fb_ids){
            if (!fb_id.equals("")) {
                FillBlanksQuestions fbqByFbId = fillBlanksQService.get_fbq_by_fb_id(Integer.parseInt(fb_id));
                if (fbqByFbId==null){
                    continue;
                }
                fbq_data.add(fbqByFbId);
            }
        }
        map.put("fbq_data", fbq_data);

        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(map);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/get_all_exam")
    @ResponseBody
    public String get_all_exam() {
        List<Exam> allExam = examService.get_all_exam();
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(allExam);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/del_exam_e_id")
    @ResponseBody
    public String del_exam_e_id(int e_id) {
        int i = examService.del_exam_e_id(e_id);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/exam_user_list")
    public String exam_user_list(int e_id, Model model) {
        model.addAttribute("e_id", e_id);
        return "views/manage/exam_user_list";
    }

    @RequestMapping("/get_exam_by_e_id_s")
    @ResponseBody
    public String get_exam_by_e_id_s(int e_id) {
        Exam examByEId = examService.get_exam_by_e_id(e_id);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(examByEId);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/get_exam_by_s_id")
    @ResponseBody
    public String get_exam_by_s_id(int s_id) {
        List<Exam> exam_by_s_id = examService.get_exam_by_s_id(s_id);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(exam_by_s_id);
        return JsonUtils.getJson(responseResult);
    }

}

